package cn.itcast.quartz.scheduler;

import cn.itcast.quartz.job.HelloJobCronTrigger;
import cn.itcast.quartz.job.HelloJobScheduler;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

public class HelloSchedulerDemoScheduler {

    public static void main(String[] args) throws Exception {
        Date start = new Date();
        start.setTime(start.getTime() + 3000);
        Date end = new Date();
        end.setTime(end.getTime() + 10000);
        //1：从工厂中获取任务调度的实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        // 2：定义一个任务调度实例，将该实例与HelloJob绑定，任务类需要实现Job接口
        JobDetail jobDetail = JobBuilder.newJob(HelloJobScheduler.class)
                .withIdentity("HelloJobScheduler", "helloJobGroup")
                .build();


        // 3：定义触发器 ，马上执行, 然后每5秒重复执行一次
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("HelloSchedulerDemoScheduler", "helloTriggerGroup")
                .startNow()
                .withSchedule(
                        CronScheduleBuilder.cronSchedule("0/3 * * * * ?")
                )
                .build();

        // 4：使用触发器调度任务的执行
        scheduler.scheduleJob(jobDetail, trigger);

        // 5：开启
        scheduler.start();

//        scheduler.standby();// 挂起
//        scheduler.shutdown();;

    }
}
